package cn.lili.modules.search.entity.dto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.utils.RegularUtil;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author paulG
 * @since 2020/10/15
 **/
@Data
public class GoodsSearchDTO {

    @ApiModelProperty(value = "关键字")
    private String keyword;

    @ApiModelProperty(value = "分类")
    private String categoryId;

    @ApiModelProperty(value = "品牌,可以多选 品牌Id@品牌Id@品牌Id") 
    private String brandId;

    @ApiModelProperty("是否为推荐商品")
    private Boolean recommend;

    @ApiModelProperty(value = "价格", example = "10_30")
    private String price;

    @ApiModelProperty(value = "属性:参数名_参数值@参数名_参数值", example = "屏幕类型_LED@屏幕尺寸_15英寸") //TODO 后续支持
    private String prop;

    @ApiModelProperty(value = "规格项列表")	//TODO 后续支持
    private List<String> nameIds;

    @ApiModelProperty(value = "卖家id，搜索店铺商品的时候使用")
    private String storeId;

    @ApiModelProperty(value = "商家分组id，搜索店铺商品的时候使用") //TODO 后续支持
    private String storeCatId;

    @ApiModelProperty(hidden = true)
    private Map<String, List<String>> notShowCol = new HashMap<>(); //TODO 后续支持

    @ApiModelProperty("当前商品skuId,根据当前浏览的商品信息来给用户推荐可能喜欢的商品")
    private String currentGoodsSkuId;

    /**
     * @see cn.lili.common.enums.PromotionTypeEnum
     */
    @ApiModelProperty("促销活动类型")
    private String promotionType; //TODO 后续支持

    @ApiModelProperty(value = "促销活动id")
    private String promotionsId; //TODO 后续支持

    /**
     * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
     */
    @ApiModelProperty(value = "商品类型")
    private String goodsType;

    @ApiModelProperty("销售模式")
    private String salesModel;

    /**
     * @see cn.lili.modules.goods.entity.enums.GoodsTypeEnum
     */
    @ApiModelProperty(value = "除了当前商品类型之外") //TODO 后续支持
    private String neGoodsType;

    @ApiModelProperty("除了销售模式当前销售模式之外") //TODO 后续支持
    private String neSalesModel; 

    //过滤搜索关键字
    public String getKeyword() {
        if (CharSequenceUtil.isNotEmpty(keyword)) {
            RegularUtil.replace(this.keyword);
        }
        return keyword;
    }
    
    //针对goods_sku表
    public QueryWrapper<GoodsSku> queryWrapper() {
        QueryWrapper<GoodsSku> queryWrapper = new QueryWrapper<>();
        if (CharSequenceUtil.isNotBlank(keyword)) {
            queryWrapper.like("goods_name", this.getKeyword());
        }
        
        if (CharSequenceUtil.isNotBlank(categoryId)) {
            queryWrapper.likeLeft("category_path", categoryId);
        }
        
        if (CharSequenceUtil.isNotBlank(storeId)) {
            queryWrapper.eq("store_id", storeId);
        }
        if (recommend != null) {
            queryWrapper.le("recommend", recommend);
        }
        
        if (CharSequenceUtil.isNotBlank(storeId)) {
            queryWrapper.eq("store_id", storeId);
        }
        
        if (CharSequenceUtil.isNotBlank(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        
        if (CharSequenceUtil.isNotBlank(goodsType)) {
            queryWrapper.eq("goods_type", goodsType);
        }
        if (CharSequenceUtil.isNotBlank(salesModel)) {
            queryWrapper.eq("sales_model", salesModel);
        }

        queryWrapper.eq("delete_flag", false);
        this.betweenWrapper(queryWrapper);
        
        return queryWrapper;
    }
    
    private <T> void betweenWrapper(QueryWrapper<T> queryWrapper) {
        if (CharSequenceUtil.isNotEmpty(price)) {
            String[] s = price.split("_");
            if (s.length > 1) {
                queryWrapper.between("price", s[0], s[1]);
            } else {
                queryWrapper.ge("price", s[0]);
            }
        }
    }
}
